import { create } from 'zustand'
import { http } from '@/utils'

import type { ResType } from '@/utils/request'

import type { PermissionResp, Menu } from '@/apis/permission'

interface LoginParams {
    username?: string,
    password?: string,
    phoneNumber?: string,
    captcha?: string
}

interface User {
    userId?: number
    accessToken?: string
    refreshToken?: string,
    expiresTime?: string,
    errMsg?: string
}

/**
 * 用户信息
 */
 export interface UserInfo {
    /**
     * 头像
     */
    avatar?: null | string;
    /**
     * 创建时间
     */
    createTime?: number | null;
    /**
     * 部门
     */
    dept?: null | Dept;
    /**
     * 部门ID
     */
    deptId?: number | null;
    /**
     * 邮箱
     */
    email?: null | string;
    /**
     * 用户id
     */
    id: number;
    /**
     * 登录时间
     */
    loginDate?: number | null;
    /**
     * 登录ip
     */
    loginIp?: null | string;
    /**
     * 手机号
     */
    mobile?: null | string;
    /**
     * 用户昵称
     */
    nickname?: null | string;
    /**
     * 岗位编号数组
     */
    postIds?: number[] | null;
    /**
     * 岗位
     */
    posts?: Post[] | null;
    /**
     * 备注
     */
    remark?: null | string;
    /**
     * 角色
     */
    roles?: Role[] | null;
    /**
     * 用户性别
     */
    sex?: number | null;
    /**
     * 社交用户
     */
    socialUsers?: Array<null | SocialUser> | null;
    /**
     * 状态
     */
    status?: number | null;
    /**
     * 用户账号
     */
    username: string;
    [property: string]: any;
}

export interface Dept {
    id?: number | null;
    name?: null | string;
    [property: string]: any;
}

export interface Post {
    /**
     * 岗位id
     */
    id?: number | null;
    /**
     * 岗位名称
     */
    name?: null | string;
    [property: string]: any;
}

export interface Role {
    /**
     * 角色id
     */
    id?: number | null;
    /**
     * 角色名称
     */
    name?: null | string;
    [property: string]: any;
}

export interface SocialUser {
    /**
     * 社交用户标识
     */
    openid?: null | string;
    /**
     * 社交用户类型
     */
    type?: number | null;
    [property: string]: any;
}


interface UserState {
    user: User | null,
    userInfo: UserInfo | null,
    menus: Menu[],
    clearUser: () => void
    fetchLogin: (by: LoginParams) => Promise<ResType<User>>,
    fetchUserInfo: () => Promise<ResType<UserInfo>>,
    fetchPermission: () => Promise<ResType<PermissionResp>>,
}

const userStore = create<UserState>((set) => ({
    user: localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user') as string) as User : null,
    userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo') as string) as UserInfo : null,
    menus: [],
    clearUser: () => {
        localStorage.removeItem('user')
        set({ user: null })
    },
    fetchLogin: async (loginParams) => {
        const res = await http<User>({
            url: 'system/auth/login', 
            method: 'post',
            data: loginParams
        })
        localStorage.setItem('user', res.data ? JSON.stringify(res.data) : '')
        localStorage.setItem('accessToken', res.data && res.data.accessToken ? res.data.accessToken : '')
        set({ user: res.data })
        return res
    },
    fetchUserInfo: async () => {
        const res = await http<UserInfo>({
            url: 'system/user/profile/get',
        })
        localStorage.setItem('userInfo', res.data ? JSON.stringify(res.data) : '')
        set({ userInfo: res.data })
        return res
    },
    fetchPermission: async () => {
        const res = await http<PermissionResp>({
            url: 'system/auth/get-permission-info',
        })
        set({ menus: res.data.menus })
        return res
    }
}))

export {
    userStore
}